#include "stdafx.h"
#include "KinExcel.h"



KinControl::KinExcel::KinExcel( void )
{
	ExApp = gcnew Excel::Application();
}


KinControl::KinExcel::~KinExcel()
{
	ExApp->Quit();
}


int KinControl::KinExcel::GetExcelValue( Excel::Worksheet^ WorkSheet,int x, int y, System::String^ %sResult )
{
	if (WorkSheet == nullptr)
	{
		return KinControl::RC_INVALIDPARAM;
	}
	sResult = (System::String^)((Excel::Range^)WorkSheet->Cells[y, x])->Text ;
	if (System::String::IsNullOrEmpty(sResult) == true)
	{
		sResult = "";
		return 2;
	}
	return RC_SUCCESS;
}

int KinControl::KinExcel::GetExcelValue( Excel::Workbook^ ExBook,int x, int y, System::String^ %sResult )
{
	if (ExBook == nullptr)
	{
		return RC_INVALIDPARAM;
	}
	return GetExcelValue((Excel::Worksheet^)ExBook->ActiveSheet,x,y,sResult);
}

int KinControl::KinExcel::SetExcelValue( Excel::Worksheet^ WorkSheet,int x, int y, System::String^ sDestString )
{
	if (WorkSheet == nullptr)
	{
		return RC_INVALIDPARAM;
	}
	WorkSheet->Cells[y, x] = sDestString;
	return RC_SUCCESS;
}

int KinControl::KinExcel::SetExcelValue( Excel::Workbook^ ExBook,int x, int y, System::String^ sDestString )
{
	if (ExBook == nullptr)
	{
		return RC_INVALIDPARAM;
	}
	return SetExcelValue((Excel::Worksheet^)ExBook->ActiveSheet, x, y, sDestString);

}

int KinControl::KinExcel::DeleteExcelLine( Excel::Worksheet^ WorkSheet,int y )
{
	if (WorkSheet == nullptr)
	{
		return RC_INVALIDPARAM;
	}
	Object^ nu = System::Reflection::Missing::Value;
	((Excel::Range^)(WorkSheet->Rows[y, nu]))->Delete(nu);
	return RC_SUCCESS;
}

int KinControl::KinExcel::DeleteExcelLine( Excel::Workbook^ ExBook,int y )
{
	if (ExBook == nullptr)
	{
		return RC_INVALIDPARAM;
	}
	return DeleteExcelLine((Excel::Worksheet^)ExBook->ActiveSheet, y);
}

int KinControl::KinExcel::GetFirstEmpyt( System::Object^ rgSource, int &nResult )
{
	nResult = 1;
	
	Object^ nu = System::Reflection::Missing::Value;
	array<System::Object^,2>^ asTemp = (array<System::Object^,2>^)((Excel::Range^)rgSource)->Value2;

	for each (System::String^ var in asTemp)
	{
		if (System::String::IsNullOrEmpty(var) == true)
		{
			break;
		}
		nResult++;
	}
	return RC_SUCCESS;
}

int KinControl::KinExcel::OpenExcel( System::String^ sFileName,Excel::Workbook^ %ResultBook)
{
	Object^ nu = System::Reflection::Missing::Value;
	return OpenExcel(sFileName, nu, nu, ResultBook);
}

int KinControl::KinExcel::OpenExcel( System::String^ sFileName, System::Object^ ReadOnly, System::Object^ Password, Excel::Workbook^ %ResultBook)
{
	if (System::IO::Path::IsPathRooted(sFileName) == false)
	{
		sFileName = System::Windows::Forms::Application::StartupPath + "\\" + sFileName;
	}

	if (System::IO::File::Exists(sFileName) == false)
	{
		return RC_NOTEXIST;
	}

	
	Object^ nu = System::Reflection::Missing::Value;

	
	ResultBook = ExApp->Workbooks->Open(sFileName,nu,ReadOnly,nu,Password,nu,nu,nu,nu,nu,nu,nu,nu,nu,nu);

	if (ResultBook == nullptr)
	{
		return RC_FAILED;
	}

	return RC_SUCCESS;
}

int KinControl::KinExcel::CloseExcel( int IsSave ,Excel::Workbook^ %wBook)
{
	Object^ nu = System::Reflection::Missing::Value;
	if (wBook != nullptr)
	{
		wBook->Close(IsSave, nu, nu);
	}
	
	wBook = nullptr;
	return RC_SUCCESS;
}

